package com.gogo.mall.service.impl;

import com.gogo.mall.entity.QyOrder;
import com.gogo.mall.dao.QyOrderDao;
import com.gogo.mall.service.QyOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * (QyOrder)表服务实现类
 *
 * @author yangjing
 * @since 2021-09-02 09:35:32
 */
@Service("qyOrderService")
public class QyOrderServiceImpl implements QyOrderService {
    @Resource
    private QyOrderDao qyOrderDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public QyOrder queryById(Integer id) {
        return this.qyOrderDao.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param qyOrder 实例对象
     * @return 实例对象
     */
    @Override
    public QyOrder insert(QyOrder qyOrder) {
        this.qyOrderDao.insert(qyOrder);
        return qyOrder;
    }

    /**
     * 修改数据
     *
     * @param qyOrder 实例对象
     * @return 实例对象
     */
    @Override
    public QyOrder update(QyOrder qyOrder) {
        this.qyOrderDao.update(qyOrder);
        return this.queryById(qyOrder.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.qyOrderDao.deleteById(id) > 0;
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param sta  状态
     * @param userId 用户id
     * @return 对象列表
     *
     */
    @Override
    public List<QyOrder> getOrdersByStateAndUserId(Integer offset, Integer userId, Integer sta){
        Integer limit=10;//默认每页10个数据
        if(offset==null)
            offset=0;
        else
            offset=(offset-1)*limit;
        List<QyOrder> orders = qyOrderDao.queryAllByLimit(userId,sta,offset,limit);
        for (QyOrder order : orders) {
            String time = order.getTime();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
            String sd = sdf.format(new Date(Long.parseLong(String.valueOf(time))));      // 时间戳转换成时间
            order.setTime(sd);
        }
        return orders;
    }
}
